package com.hex.ds.hdrs.common.utils;

import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;

import java.util.*;

public class ListUtil {

    public ListUtil() {
    }

    public static void removeDuplicate(List list) {
        HashSet set = new HashSet(list);
        list.clear();
        list.addAll(set);
    }

    public static boolean isEmpty(List[] lists) {
        if (lists == null) {
            return true;
        } else {
            List[] arr$ = lists;
            int len$ = lists.length;

            for (int i$ = 0; i$ < len$; ++i$) {
                List list = arr$[i$];
                if (list == null || list.size() == 0) {
                    return true;
                }
            }

            return false;
        }
    }

    public static boolean isEmpty(List list) {
        return list == null || list.size() == 0;
    }

    public static String[] listToStringArray(List<String> list) {
        if (isEmpty(list)) {
            return null;
        } else {
            String[] arrs = new String[list.size()];
            list.toArray(arrs);
            return arrs;
        }
    }

    public static boolean contains(List list, Object value) {
        Iterator i$ = list.iterator();

        Object object;
        do {
            if (!i$.hasNext()) {
                return false;
            }

            object = i$.next();
        } while (!object.equals(value));

        return true;
    }

    public static List minus(List list1, List list2) {
        List retList = new ArrayList();
        Iterator i$ = list1.iterator();

        while (i$.hasNext()) {
            Object table = i$.next();
            if (!contains(list2, table)) {
                retList.add(table);
            }
        }

        return retList;
    }

    public static void printList(List list) {
        Iterator i$ = list.iterator();

        while (i$.hasNext()) {
            Object obj = i$.next();
            System.out.println(obj);
        }

    }

    public static List List2Map(List list, String key) {
        List retList = new ArrayList();
        Iterator i$ = list.iterator();

        while (i$.hasNext()) {
            Object obj = i$.next();
            Map map = new HashMap();
            map.put(key, obj);
            retList.add(map);
        }

        return retList;
    }

    /**
     * @Method: listToString
     * @Param: [list]
     * @Return: java.lang.String
     * @Description: 集合转换为逗号分隔的字符串
     * @Author: ji.tang
     * @Date: 2022/3/31 17:09
     * @Version: V1.0
     */
    public static String listToString(List<String> list){
        if (!CollectionUtils.isEmpty(list)){
            String str = String.join(",", list);
            return str;
        }
        return "";
    }

    public static void main(String[] args) {
        //冒泡排序算法
        int[] numbers = new int[]{1, 5, 2, 4, 3};
        //需进行length-1次冒泡
        for (int i = 0; i < numbers.length - 1; i++) {
            for (int j = 0; j < numbers.length - 1 - i; j++) {
                if (numbers[j] > numbers[j + 1]) {
                    int temp = numbers[j];
                    numbers[j] = numbers[j + 1];
                    numbers[j + 1] = temp;
                }
            }
        }
        System.out.println("从小到大排序后的结果是:");
        for (int i = 0; i < numbers.length; i++) {
            System.out.print(numbers[i] + " ");
        }
    }
}
